package org.example.java8time;

import org.junit.Test;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;

/**
 * @Classname DateUtilsTest
 * @Description 测试DateUtils方法demo
 * 如果是 JDK8 的应用，可以使用
 * Instant 代替 Date，
 * LocalDateTime 代替 Calendar，
 * DateTimeFormatter 代替 SimpleDateFormat
 * @Date 2021/2/4 3:39 下午
 * @Created by xiangjd
 */
public class DateUtilsTest {


    /**
     * date相关转化测试
     */
    @Test
    public void test1() {
        Date date = new Date();

        LocalDateTime localDateTime = DateUtils.date2LocalDateTime(date);
        LocalDate localDate = DateUtils.date2LocalDate(date);
        LocalTime localTime = DateUtils.date2LocalTime(date);
        Date newDate = DateUtils.localDateTime2Date(localDateTime);

        System.out.println("date: " + date);
        System.out.println("localDateTime: " + localDateTime);
        System.out.println("localDate: " + localDate);
        System.out.println("localTime: " + localTime);
        System.out.println("newDate: " + newDate);
    }


    /**
     * 解析 (String转化为相关类型)
     */
    @Test
    public void test2() {
        String dateStr = "2019-12-08";
        String dateTimeStr = "2019-12-08 17:14:12";
        String timeStr = "17:14:12";

        LocalDate localDate = DateUtils.parseLocalDate(dateStr, DateUtils.YMD);
        LocalTime localTime = DateUtils.parseLocalTime(timeStr, DateUtils.HMS);
        LocalDateTime localDateTime = DateUtils.parseLocalDateTime(dateTimeStr, DateUtils.YMD_HMS);
        Instant instant = DateUtils.parseInstant(dateTimeStr, DateUtils.YMD_HMS);
        Date date = DateUtils.parseDate(dateTimeStr, DateUtils.YMD_HMS);

        System.out.println("localDate: " + localDate);
        System.out.println("localTime: " + localTime);
        System.out.println("localDateTime: " + localDateTime);
        //注意instant是格林威治时间，和北京时间相差8小时。
        System.out.println("instant: " + instant);
        System.out.println("date: " + date);
    }

    /**
     * 格式化  locaDate.localTime.LocalDateTime.instant.date转化成string
     */
    @Test
    public void test3() {
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        Instant instant = Instant.now();
        Date date = new Date();

        String localDateStr = DateUtils.formatTemporal(localDate, DateUtils.CN_YMD);
        String localTimeStr = DateUtils.formatTemporal(localTime, DateUtils.CN_HMS);
        String localDateTimeStr = DateUtils.formatTemporal(localDateTime, DateUtils.CN_YMD_HMS);
        String instantStr = DateUtils.formatTemporal(instant, DateUtils.CN_YMD_HMS);
        String dateStr = DateUtils.formatDate(date, DateUtils.CN_YMD_HMS);

        System.out.println("localDate: " + localDateStr);
        System.out.println("localTime: " + localTimeStr);
        System.out.println("localDateTime: " + localDateTimeStr);
        System.out.println("instant: " + instantStr);
        System.out.println("date: " + dateStr);
    }

    /**
     * 获取本日最早/最晚时间，昨日最早/最晚时间，本周最早/最晚时间，本月最早/最晚时间，本年最早/最晚时间，
     */
    @Test
    public void test4() {
        Date todayStart = DateUtils.getTodayStart();
        Date todayEnd = DateUtils.getTodayEnd();
        Date yesterdayStart = DateUtils.getYesterdayStart();
        Date yesterdayEnd = DateUtils.getYesterdayEnd();
        Date weekStart = DateUtils.getWeekStart();
        Date weekEnd = DateUtils.getWeekEnd();
        Date monthStart = DateUtils.getMonthStart();
        Date monthEnd = DateUtils.getMonthEnd();
        Date yearStart = DateUtils.getYearStart();
        Date yearEnd = DateUtils.getYearEnd();

        System.out.println("todayStart: " + todayStart);
        System.out.println("todayEnd: " + todayEnd);
        System.out.println("yesterdayStart: " + yesterdayStart);
        System.out.println("yesterdayEnd: " + yesterdayEnd);
        System.out.println("weekStart: " + weekStart);
        System.out.println("weekEnd: " + weekEnd);
        System.out.println("monthStart: " + monthStart);
        System.out.println("monthEnd: " + monthEnd);
        System.out.println("yearStart: " + yearStart);
        System.out.println("yearEnd: " + yearEnd);
    }
}
